'use strict'

class adminlogin {

  constructor() {
    //param默认就是/:id，若要改变路由，则可以设置此属性，比如设置为/:name
    this.param = '/'
  }

  //创建资源
  async post(c) {
    var id, password
    //根据content-type分支处理
    if (!c.body.adminname) {
      let obj = JSON.parse(c.body)
      id = obj.adminname
      password = obj.adminpassword
      console.log(id, password)
    } else {
      id = c.body.adminname
      password = c.body.adminpassword
      console.log(id, password)
    }
    //数据库查询
    let ret = await c.service.psql.query(`select manage_pwd from manage where manage_account like '${id}'`)
    //返回分支
    if (ret.rows[0]) {
      if (password == ret.rows[0].manage_pwd) {
        let userinfo = {
          id: id,
          password: password
        }
        let token = c.service.tok.make(userinfo)
        c.status(200).send({
          token: token
        })
      } else {
        c.status(403).send('登录失败')
      }
    } else {
      c.status(500).send('登录失败')
    }
  }

}

module.exports = adminlogin
